package com.ysz.learn.selflearn.aspect;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: yushizhong
 * @date: 2021-10-28 16:55
 * @version: 1.0
 **/
@Component
@Aspect
@Slf4j
public class LogAspect {
    @Pointcut("execution(* com.ysz.learn.selflearn.controller.*.*(..))")
    void loginLog() {
        log.info("进入切点");

    }

    @Before("loginLog()")
    void loginBefore(JoinPoint joinPoint) {
        log.info("进入前置通知");
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("请求路径 : " + request.getRequestURL());
        System.out.println("请求方式 : " + request.getMethod());
        System.out.println("方法名 : " + joinPoint.getSignature().getName());
        System.out.println("类路径 : " + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("参数 : " + Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(returning = "returnObi", pointcut = "loginLog()")
    void loginAfter(Object returnObi) {
        log.info("返回值{}", returnObi);

    }

}
